﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using FinalProject.UI.Controls;


namespace FinalProject.UI
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private IList<HTabItem> listTab;
        public MainWindow()
        {
            InitializeComponent();
            this.InitialEvents();
            this.listTab = new List<HTabItem>();
            //this.AddTab(new SelectionTypeClass() { Key = "OrderManagerPage", SelectedType = SelectionTypeClass.AddingType.OrderDocument, TabHeader = "Order Document" });
        }

        private void AddTab(SelectionTypeClass selectedObj)
        {
            if (selectedObj == null || string.IsNullOrEmpty(selectedObj.Key))
                throw new Exception("Tab's key cannot be null");

            var existTab = listTab.Where(p => p.Name == selectedObj.Key).SingleOrDefault();
            IBasePage content = null;
            if (existTab == null)
            {
                HTabItem tabItem = new HTabItem()
                {
                    Header = selectedObj.TabHeader,
                    IsSelected = true
                };
                tabItem.Name = selectedObj.Key;
                switch (selectedObj.SelectedType)
                {
                    case SelectionTypeClass.AddingType.Customer:
                        content = new CustomerPage();
                        break;
                    case SelectionTypeClass.AddingType.OrderDocument:
                        content = new OrderPage();
                        break;
                    case SelectionTypeClass.AddingType.DeliveryDocument:
                        content = new DeliveryPage();
                        break;
                    case SelectionTypeClass.AddingType.InvoiceDocument:
                        content = new InvoicePage();
                        break;
                    case SelectionTypeClass.AddingType.VoucherNote:
                        content = new VoucherPage();
                        break;
                    default:
                        break;
                }
                if (content != null)
                    content.ChangeToNewMode();
                tabItem.Content = content;
                tabItem.FormToEditModeEvent += new HTabItem.FormToEditMode(tabItem_FormToEditModeEvent);
                this.TabRoot.Items.Add(tabItem);
                this.listTab.Add(tabItem);
            }
            else
            {
                content = existTab.Content as IBasePage;
                if (content != null)
                    content.ChangeToNewMode();
            }
        }

        #region **Events**

        private void InitialEvents()
        {
            this.TabRoot.SelectionChanged += new SelectionChangedEventHandler(TabRoot_SelectionChanged);
            this.btnCancelModified.Click += new RoutedEventHandler(btnCancelModified_Click);
        }

        private void btnCancelModified_Click(object sender, RoutedEventArgs e)
        {
            MessageBoxResult result = Utilities.GetMessageWarning("Do you want cancel your modified?");
            if (result == MessageBoxResult.Yes)
            {
                IBasePage selectedPage = this.GetSelectedFunction();
                if (selectedPage != null)
                {
                    selectedPage.CancelModified();
                }
            }
        }

        private void TabRoot_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            HTabItem hTabItem = this.GetSeletectedTab();
            if (hTabItem != null)
            {
                bool enable = hTabItem.InEditMode;
                this.AppSaveItem.IsEnabled = !enable;
                this.btnCancelModified.IsEnabled = enable;
            }
        }

        private void tabItem_FormToEditModeEvent(object sender, EventArgs e)
        {
            HTabItem selectedTab = this.GetSeletectedTab();
            bool enable = selectedTab.InEditMode;
            this.AppSaveItem.IsEnabled = enable;
            this.AppNewItem.IsEnabled = !enable;

            this.btnCancelModified.IsEnabled = enable;
        }

        private void btnDelete_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            // TODO: Add event handler implementation here.
        }

        private void btnCopy_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            // TODO: Add event handler implementation here.
        }

        private void AppNewItem_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            AddingNewForm frmAdd = new AddingNewForm();
            frmAdd.Closed += new EventHandler(frmAdd_Closed);
            frmAdd.ShowDialog();
        }

        private void frmAdd_Closed(object sender, EventArgs e)
        {
            try
            {
                AddingNewForm frmAdd = sender as AddingNewForm;
                if (frmAdd == null) return;

                var selectedType = frmAdd.SelectedObjectType;
                if (selectedType != null)
                    this.AddTab(selectedType);
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }

        private void AppSaveItem_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            // TODO: Add event handler implementation here.
            IBasePage comFunc = this.GetSelectedFunction();
            if (comFunc != null)
            {
                comFunc.SaveData();
            }
        }

        #endregion

        private HTabItem GetSeletectedTab()
        {
            if (this.TabRoot == null || this.TabRoot.SelectedItem == null) return null;
            HTabItem result = this.TabRoot.SelectedItem as HTabItem;
            return result;
        }

        private IBasePage GetSelectedFunction()
        {
            HTabItem selectedTab = this.TabRoot.SelectedItem as HTabItem;
            if (selectedTab == null) return null;
            IBasePage commFunc = null;
            if (selectedTab.Content is IBasePage)
            {
                commFunc = selectedTab.Content as IBasePage;
            }
            return commFunc;
        }

    }
}
